package com.tledu.store.aspect;

import com.tledu.common.utils.DateUtils;
import com.tledu.common.utils.SecurityUtils;
import com.tledu.store.domain.StoreHouse;
import com.tledu.store.domain.StoreLog;
import com.tledu.store.mapper.StoreLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: 369
 * @CreateTime: 2024-08-30  09-44
 */
@Aspect
@Component
@Slf4j
public class LogAutoFullAspect {
    @Autowired
    private StoreLogMapper storeLogMapper;

    @Pointcut("execution(* com.tledu.store.mapper.StoreHouseMapper.*(..)) && @annotation(com.tledu.store.aspect.MyLog)") // 切点表达式，根据你的实际包路径设置
    public void serviceLayer() {}

    @Around("serviceLayer()")
    public Object logBefore(ProceedingJoinPoint pjp) throws Throwable {
        log.info("库房操作日志开始添加....");
        Object result = pjp.proceed();
        //获取到当前被拦截的方法上的数据库操作类型
        MethodSignature signature = (MethodSignature) pjp.getSignature();//方法签名对象
        MyLog autoFill = signature.getMethod().getAnnotation(MyLog.class);//获得方法上的注解对象
        OperationType operationType = autoFill.value();//获得数据库操作类型

        //获取到当前被拦截的方法的参数--实体对象
        Object[] args = pjp.getArgs();
        if(args == null || args.length == 0){
            return null;
        }
        //准备赋值的数据
        Long currentId = SecurityUtils.getUserId();

        //批量插入
//        List<com.tledu.store.aspect.MyLog> logList = new ArrayList<>();
        if(operationType == OperationType.INSERT){
            StoreLog storeLog =  new StoreLog();
                StoreHouse entity = (StoreHouse) args[0];

                storeLog.setStoreId(entity.getId());
                storeLog.setOperationType(0L);
                storeLog.setCreateUser(currentId);
                storeLog.setCreateTime(DateUtils.getNowDate());
                storeLog.setDelFlag(0L);
                storeLogMapper.insertStoreLog(storeLog);

        }else if(operationType == OperationType.UPDATE){
            StoreLog storeLog = new StoreLog();

            StoreHouse entity = (StoreHouse) args[0];
            storeLog.setStoreId(entity.getId());
            storeLog.setOperationType(1L);
            storeLog.setCreateUser(currentId);
            storeLog.setCreateTime(DateUtils.getNowDate());
            storeLog.setDelFlag(0L);
                storeLogMapper.insertStoreLog(storeLog);

        }else {
            StoreLog storeLog = new StoreLog();
            Long[] ids = (Long[]) args[0];
            for (Long id : ids) {
                storeLog.setStoreId(id);
                storeLog.setOperationType(2L);
                storeLog.setCreateUser(currentId);
                storeLog.setCreateTime(DateUtils.getNowDate());
                storeLog.setDelFlag(0L);
                storeLogMapper.insertStoreLog(storeLog);
            }
        }
//        storeLogMapper.insertBatch(logList);
        return result;

    }
}
